package com.example.lambda.date;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

public class Test2 {

    // LocalDate LocalTime LocalDateTime
    // Instant 时间戳

    @Test
    public void test(){
        LocalDateTime ldt =LocalDateTime.now();
        System.out.println(ldt);
        LocalDateTime ldt2 = ldt.plusYears(2);
        System.out.println(ldt2);
    }

    @Test
    public void test2(){
        Instant is = Instant.now();
        System.out.println(is); // 默认utc时间
        OffsetDateTime odf = is.atOffset(ZoneOffset.ofHours(8));
        System.out.println("---"+odf);
    }


    // 时间间隔
    @Test
    public void test3()  {
        Instant is1 = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant is2 = Instant.now();
        Duration d = Duration.between(is1,is2);
        System.out.println(d.getSeconds());
    }


    // 日期间隔
    @Test
    public void test4(){
        LocalDate ld1 = LocalDate.of(2015,5,16);
        LocalDate ld2 = LocalDate.now();
        Period.between(ld1,ld2);
    }

  // 时间校正器
    @Test
    public void test5(){
        LocalDateTime date = LocalDateTime.now();
        System.out.println(date);

        LocalDateTime ldt = date.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
    }

    // DateTimeFormatter
    @Test
    public void test6(){
        DateTimeFormatter dtf = DateTimeFormatter.BASIC_ISO_DATE;
        LocalDateTime ldt = LocalDateTime.now();
        String time = ldt.format(dtf);
        System.out.println(time);


        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String time2 = ldt.format(dtf2);
        System.out.println(time2);
    }


    // 时区 zonedDate zonedTime zonedDateTime
    @Test
    public void test7(){
        Set<String> time = ZoneId.getAvailableZoneIds();
        time.forEach(System.out::println);   // 获取所有时区

    }
    @Test
    public void test8(){
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Africa/Cairo"));  //指定时区
        System.out.println(ldt);
    }
}
